package cn.com.dstz.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONArray;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import cn.com.dstz.base.action.BaseAction;
import cn.com.dstz.ipseek.IPSeeker;
import cn.com.dstz.utils.ActionUtils;
import cn.com.dstz.utils.CookieUtils;
import cn.com.dstz.utils.FillNameQuery;
import cn.com.dstz.utils.NumberUtils;
import cn.com.dstz.web.constants.Constants;

/**
 * 首页及其他
 * @author 叶装装
 *
 */
public class IndexAct extends BaseAction{

	private static final String TREE_COOKIE = "tree_";
	
	public void left( ){
		
		List< Map< String, Object >> purviewList = this.getEntityInSession( Constants.ADMIN_PURVIEW );
		
		List< Map< String, Object >> treeList = new ArrayList< Map< String, Object >> ( );
		
		treeList.add( ActionUtils.getNodes( 99999999, 0, "管理菜单", "", true, false, null ));
		
		for( Map< String, Object > purview : purviewList ){
			Object id = purview.get( "id" );
			Object name = purview.get( "name" );
			Object url = getRequest( ).getContextPath( ) + purview.get( "url" );
			Integer parentid = ( Integer ) purview.get( "parentid" );
			Byte isDisplay = ( Byte ) purview.get( "isdisplay" );
			
			if( parentid == 0 ){
				parentid = 99999999;
			}
			if( isDisplay == 1 ){
				boolean open = false;
				
				String value = CookieUtils.getCookieValue( getRequest(), TREE_COOKIE + id );
				if( value != null ){
					open = NumberUtils.getBoolean( value );
				}
		
				treeList.add( ActionUtils.getNodes( id, parentid, name, url, open, false, "rightFrame" ) );
			}
		}
		
		this.ajaxResponse( JSONArray.fromObject( treeList ) );
		return;
		
	}
	
	public String tonotice ( ){
		
		String sql = "select g.*, a.username, n.name,n.level from dstzgg g, dstzadmin a, dstz_noticetype n where g.adminid = a.id and g.ggtype = n.id and g.id=:id";
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		String id = getParameter( "id" );
		valueMap.put( "id", id );
		Map< String, Object > data = baseService.queryForMap( sql, valueMap );
		
		if( data == null || data.isEmpty( ) ){
			this.alertMsg( null );
		}
		
		getRequest( ).setAttribute( "data", data );
		
		return "notice";
	}
	
	public String top( ){
		// 查询最近5条系统公告信息
		String sql = "select * from dstzgg where ggtype=:ggtype order by sendtime desc ";
		List< Map< String, Object >> noticeList = baseService.queryList( sql, 0, 5, FillNameQuery.fillValue( sql, 1 ) );
		
		if( noticeList != null && noticeList.size( ) > 0 ){
			for( Map< String, Object > notice : noticeList ){
				String content = ( String ) notice.get( "content" );
				content = Jsoup.clean( content, Whitelist.none( ) );
				int len = content.length( );
				
				if( len > 30 ){
					content = content.substring( 0, 30 ) + "...";
				}
				notice.put( "content", content );
			}
		}
		getRequest( ).setAttribute( "noticeList", noticeList );
		
		return "top";
	}
	
	
	public String index( ){
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		String sql = "select * from dstz_loginlog where loginuid=" + admin.get( "id" ) + " order by logindate desc limit 0,1";
		Map< String, Object > login = baseService.getData( sql );
		login.put( "area", ipSeeker.getIPLocation( login.get( "loginip" ).toString( ) ) );
		
		getRequest( ).setAttribute( "login", login );
		
		int level = ActionUtils.getAdminLevel( admin );
		
		if( level == 0 ){
			setSystemProperties( );
			setSystemCount( );
		}
		
		return "index";
	}
	
	private void setSystemCount( ){
		this.setSystemCountToRequest( "usercount", "select count(*) as a from dstzmember", null );
		this.setSystemCountToRequest( "admincount", "select count(*) as a from dstzadmin", null );
		this.setSystemCountToRequest( "agentcount", "select count(*) as a from dstzagent", null );
		this.setSystemCountToRequest( "categorycount", "select count(*) as a from dstzcategory", null );
		
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		valueMap.put( "status", 1 );
		
		this.setSystemCountToRequest( "tradecount", "select count(*) as a from dstztrade where status=:status", valueMap );
		
		valueMap.put( "status", 2 );
		
		this.setSystemCountToRequest( "closetradecount", "select count(*) as a from dstztrade where status=:status", valueMap );
		
		this.setSystemCountToRequest( "analysiscount", "select count(*) as a from dstzfx", valueMap );
		this.setSystemCountToRequest( "noticecount", "select count(*) as a from dstzgg", valueMap );
		
		
	}
	
	private void setSystemCountToRequest( String name, String sql, Map< String, Object > valueMap ){
		int count = baseService.countBySQL( sql, valueMap );
		getRequest( ).setAttribute( name, count );
		
	}
	
	private void setSystemProperties( ){
		
		Properties props = System.getProperties();
		Runtime runtime = Runtime.getRuntime();
		long freeMemoery = runtime.freeMemory();
		long totalMemory = runtime.totalMemory();
		long usedMemory = totalMemory - freeMemoery;
		long maxMemory = runtime.maxMemory();
		long useableMemory = maxMemory - totalMemory + freeMemoery;
		
		getRequest( ).setAttribute("props", props);
		getRequest( ).setAttribute("freeMemoery", NumberUtils.numFormat( "0.00", freeMemoery / 1024.0 / 1024.0 )   );
		getRequest( ).setAttribute("totalMemory", NumberUtils.numFormat( "0.00", totalMemory / 1024.0 / 1024.0 ) );
		getRequest( ).setAttribute("usedMemory", NumberUtils.numFormat( "0.00", usedMemory / 1024.0 / 1024.0 ) );
		getRequest( ).setAttribute("maxMemory", NumberUtils.numFormat( "0.00", maxMemory / 1024.0 / 1024.0 ) );
		getRequest( ).setAttribute("useableMemory", NumberUtils.numFormat( "0.00", useableMemory / 1024.0 / 1024.0 ) );
	}
	
	private static final long serialVersionUID = 1L;
	
	private IPSeeker ipSeeker;

	public void setIpSeeker( IPSeeker ipSeeker ) {
		this.ipSeeker = ipSeeker;
	}

}
